package org.dromara.oce.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.oce.domain.qo.OceTitleQo;
import org.dromara.oce.domain.bo.OceTitleBo;
import org.dromara.oce.domain.vo.OceTitleVo;
import org.dromara.oce.domain.OceTitle;
import org.dromara.oce.mapper.OceTitleMapper;
import org.dromara.oce.service.OceTitleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 巨量广告标题、产品卖点、行动号召Service业务层处理
 *
 * @author dsbb
 * @date 2025-06-11
 */
@RequiredArgsConstructor
@Service
public class OceTitleServiceImpl extends ServiceImpl<OceTitleMapper,OceTitle> implements OceTitleService {

    private final OceTitleMapper baseMapper;

    /**
     * 查询巨量广告标题、产品卖点、行动号召
     *
     * @param id 主键
     * @return 巨量广告标题、产品卖点、行动号召
     */
    @Override
    public OceTitleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询巨量广告标题、产品卖点、行动号召列表
     *
     * @param qo        查询条件
     * @param pageQuery 分页参数
     * @return 巨量广告标题、产品卖点、行动号召分页列表
     */
    @Override
    public TableDataInfo<OceTitleVo> queryPageList(OceTitleQo qo, PageQuery pageQuery) {
        LambdaQueryWrapper<OceTitle> lqw = buildQueryWrapper(qo);
        Page<OceTitleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的巨量广告标题、产品卖点、行动号召列表
     *
     * @param qo 查询条件
     * @return 巨量广告标题、产品卖点、行动号召列表
     */
    @Override
    public List<OceTitleVo> queryList(OceTitleQo qo) {
        LambdaQueryWrapper<OceTitle> lqw = buildQueryWrapper(qo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceTitle> buildQueryWrapper(OceTitleQo qo) {
        LambdaQueryWrapper<OceTitle> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(qo.getName()), OceTitle::getName, qo.getName());
        lqw.eq(StringUtils.isNotBlank(qo.getType()), OceTitle::getType, qo.getType());
        lqw.orderByDesc(OceTitle::getId);
        return lqw;
    }

    /**
     * 新增巨量广告标题、产品卖点、行动号召
     *
     * @param bo 巨量广告标题、产品卖点、行动号召
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OceTitleBo bo) {
        OceTitle add = MapstructUtils.convert(bo, OceTitle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改巨量广告标题、产品卖点、行动号召
     *
     * @param bo 巨量广告标题、产品卖点、行动号召
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OceTitleBo bo) {
        OceTitle update = MapstructUtils.convert(bo, OceTitle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceTitle entity){
    }

    /**
     * 校验并批量删除巨量广告标题、产品卖点、行动号召信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
